/**
 * @author erond
 * @class Classe adibita all'avvio di un Client FTP
 */

package commonUtility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.commons.net.ftp.FTPClient;

/**
 * Classe adibita all'avvio di un Client FTP
 */
public class FtpClientThread extends Thread{
	
	private FTPClient ftp = null;
	private InetAddress host = null;
	private int port = 0;
	private String source_name = null;
	private String dest_name = null;

	/**
	 * @param host Indirizzo IP del server FTP
	 * @param port Porta del server FTP alla quale collegarsi
	 * @param source_name Path completo del file da inviare
	 * @param dest_name Nome del file che il server assegnerà al file ricevuto
	 */
	public FtpClientThread(InetAddress host,int port,String source_name,String dest_name) {
		this.host=host;
		this.port=port;
		ftp=new FTPClient();
		this.source_name=source_name;
		this.dest_name=dest_name;
	}
	
	public boolean sendFile() {
		File file = new File(source_name);
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		boolean esito = false;
		try {
			esito = ftp.storeFile(dest_name,fis);
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return esito;
	}
	
	public void run() {
		
		System.err.println(Thread.currentThread().getName()+" avviato su: \n indirizzo: "+host+"\n porta: "+port);
		
		try {
			// connessione al server e login come utente anonymous
			ftp.connect(host,port);
			ftp.login("anonymous","anonymous");
			System.out.println("Connessione a " +host+ ". Esito: "+ftp.getReplyString());
			
			//Invio il file finchè non ottengo esito positivo
			boolean esito = false;
			int count = 0;
			while(!esito) {
				esito=sendFile();
				System.out.println(Thread.currentThread().getName()+": Esito del trasferimento: "+esito);
				count++;
				if (count==5) {
					System.err.println(Thread.currentThread().getName()+": "+count+" tentativi di invio falliti. Chiudo!");
					break;
				}
			}
			
			// Logout from the FTP Server and disconnect
			ftp.logout();
			ftp.disconnect();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}//fine run
	
	//main per i test
	public static void main(String args[]) {
		
		InetAddress masterIP = null;
		InetAddress slaveIP = null;
		InetAddress master_esternoIP = null;
		try {
			masterIP= InetAddress.getByName("localhost");
			slaveIP = InetAddress.getByName("localhost");
			master_esternoIP = InetAddress.getByName("192.168.178.56");
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		FtpClientThread f_master = new FtpClientThread(masterIP, 2121, "./slaveTmp/ciao_from_slave.txt", "ciao_from_slave.txt");
		f_master.start();
		
		FtpClientThread f_slave = new FtpClientThread(slaveIP, 2122, "./gatewayTmp/ciao_from_master.txt", "ciao_from_master.txt");
		f_slave.start();
		
		FtpClientThread f_master_esterno = new FtpClientThread(master_esternoIP, 2121, "./slaveTmp/ciao_from_slave.txt", "ciao_from_roberto.txt");
		f_master_esterno.start();
		
	}
	
}
